<template>
  <div class="guns-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="户主姓名:">
              <a-input
                v-model:value.trim="where.householdOwnerName"
                placeholder="请输入户主姓名"
                allow-clear
              />
            </a-form-item>
            <a-form-item label="家庭成员姓名:">
              <a-input
                v-model:value.trim="where.householdMemberName"
                placeholder="请输入家庭成员姓名"
                allow-clear
              />
            </a-form-item>
            <a-form-item label="家庭成员人数:">
              <a-input
                v-model:value.trim="where.householdSizeFrom"
                type="number"
                placeholder="最小值"
                class="range-form-item"
              />
            </a-form-item>
            <a-form-item label="~" class="range-form-label">
              <a-input
                v-model:value.trim="where.householdSizeTo"
                type="number"
                placeholder="最大值"
                class="range-form-item"
              />
            </a-form-item>
            <a-form-item label="房屋总面积:">
              <a-input
                v-model:value.trim="where.householdAreaFrom"
                type="number"
                placeholder="最小值"
                class="range-form-item"
              />
            </a-form-item>
            <a-form-item label="~" class="range-form-label">
              <a-input
                v-model:value.trim="where.householdAreaTo"
                type="number"
                placeholder="最大值"
                class="range-form-item"
              />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false" class="table-height">
        <BasicTable
          :canResize="false"
          ref="tableRef"
          :api="HouseholdApi.findPage"
          :where="where"
          :columns="columns"
          showTableSetting
          rowKey="householdId"
          :rowSelection="{
            type: 'checkbox',
            selectedRowKeys: checkedKeys,
            onChange: onSelectChange,
          }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <div class="table-toolbar">
              <a-space>
                <a-button type="primary" @click="openEdit()">
                  <template #icon>
                    <plus-outlined />
                  </template>
                  <span>新增</span>
                </a-button>
              </a-space>
            </div>
          </template>

          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex == 'householdOwnerName'">
              <a @click="openShow(record)">{{ record.householdOwnerName }}</a>
            </template>

            <!-- table操作栏按钮 -->
            <template v-else-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">编辑</a>
                <a @click="openShow(record)">查看</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要删除此权属户吗？" @confirm="remove(record)">
                  <a class="guns-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </a-card>
    </div>

    <!-- 编辑弹窗 -->
    <household-edit
      v-model:visible="showEdit"
      :data="current"
      :isUpdate="isUpdate"
      :editable="editable"
      @done="reload"
    />
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import HouseholdEdit from './household-edit.vue';
  import { HouseholdApi } from '/@/api/business/household/HouseholdApi';
  import { message } from 'ant-design-vue';
  // 查询条件
  const where = reactive({
    householdOwnerName: '',
    householdMemberName: '',
    householdSizeFrom: null,
    householdSizeTo: null,
    householdAreaFrom: null,
    householdAreaTo: null,
  });

  // 表格列配置
  const columns = ref<string[]>([
    {
      title: '户主姓名',
      dataIndex: 'householdOwnerName',
      width: 160,
    },
    {
      title: '户编号',
      dataIndex: 'householdNo',
      width: 160,
    },
    {
      title: '户所在地',
      dataIndex: 'householdAddress',
      width: 160,
    },
    {
      title: '家庭成员总人数（人）',
      dataIndex: 'householdSize',
      width: 160,
    },
    {
      title: '房屋总面积（㎡）',
      dataIndex: 'householdArea',
      align: 'center',
      width: 160,
    },
    {
      title: '操作',
      key: 'action',
      width: 180,
      align: 'center',
    },
  ]);

  // 多选选中列表
  const checkedKeys = ref<Array<string | number>>([]);

  // 当前行数据
  const current = ref<any>(null);

  // 是否显示新增编辑弹框
  const showEdit = ref<boolean>(false);

  // 是否是更新权属户
  const isUpdate = ref<boolean>(false);

  // 是否是可编辑权属户
  const editable = ref<boolean>(true);

  // ref
  const tableRef = ref(null);

  /**
   * 点击查询
   * @author: lichenhui
   * @date: 2024-03-21 11:07
   */
  const reload = () => {
    checkedKeys.value = [];
    tableRef.value.reload({ page: 1 });
  };

  /**
   * 点击重置
   * @author: lichenhui
   * @date: 2024-03-21 11:07
   */
  const reset = () => {
    where.householdOwnerName = '';
    where.householdMemberName = '';
    where.householdSizeFrom = null;
    where.householdSizeTo = null;
    where.householdAreaFrom = null;
    where.householdAreaTo = null;
    reload();
  };

  // 表格选中改变
  const onSelectChange = (selectedRowKeys: (string | number)[]) => {
    checkedKeys.value = selectedRowKeys;
  };

  /**
   * 打开编辑弹窗
   *
   * @author: lichenhui
   * @date: 2024-03-21 11:07
   */
  const openEdit = (row) => {
    current.value = row;
    showEdit.value = true;
    isUpdate.value = !!row;
    editable.value = true;
  };

  /**
   * 打开查看弹窗
   *
   * @author: lichenhui
   * @date: 2024-03-21 11:07
   */
  const openShow = (row) => {
    current.value = row;
    showEdit.value = true;
    isUpdate.value = true;
    editable.value = false;
  };

  /**
   * 删除单个
   *
   * @author: lichenhui
   * @date: 2024-03-21 11:07
   */
  const remove = async (row: any) => {
    const result = await HouseholdApi.del({ householdId: row.householdId });
    message.success(result.message);
    reload();
  };
</script>

<style lang="less">
  .range-form-label .ant-form-item-label > label:after {
    content: '';
  }
</style>

<style lang="less" scoped>
  .range-form-item {
    width: 90px;
  }
  .ant-card-body {
    padding-bottom: 14px;
  }
  .ant-form-inline .ant-form-item {
    margin-bottom: 10px;
  }
</style>
